import { _decorator, Camera, Component, Node, UITransform, Vec3 } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('ParallaxBackGround')
export class ParallaxBackGround extends Component {
    @property(Node) cam: Node = null;
    @property parallaxEffect: number = 8;
    xPosition: number = 0;
    length:number = 0;
    start() {
        this.length = this.node.getComponent(UITransform).width;
        this.xPosition = this.node.getPosition().x;
    }

    update(deltaTime: number) {
        let distanceMove = this.cam.getPosition().x * (1-this.parallaxEffect);
        let distanceToMove = this.cam.getPosition().x * this.parallaxEffect;
        this.node.setPosition(new Vec3(this.xPosition + distanceToMove , this.node.getPosition().y, 0));
    
        if(distanceMove > this.xPosition + this.length){
            this.xPosition = this.xPosition + this.length;
        }else if(distanceMove < this.xPosition - this.length){
            this.xPosition = this.xPosition - this.length;
        }
    }
}


